<?php
namespace App\Middleware;

use HyperfSanctum\Manage;
use App\Exception\ErrorException;

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

use App\Service\Limit\LimitService;

// 用户限流器
class UserLimit implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // 限流未通过
        if(!LimitService::pass(Manage::user()->id))
        {
            throw new ErrorException('qps_limit');
        }
        
        return $handler->handle($request);
    }
}